﻿using System;
using System.Diagnostics;

namespace JanusLibAing
{
    public class TimeTracer : IDisposable
    {
        private readonly string text;
        private readonly bool enableTrace;
        Stopwatch timer = new Stopwatch();

        #region Implementation of IDisposable

        public TimeTracer(string text, bool enableTrace)
        {
            this.text = text;
            this.enableTrace = enableTrace;
            timer.Start();
            Trace.WriteLine(text + " start");
        }

        public void TraceTime(string finishText)
        {
            if (enableTrace)
            {
                Trace.WriteLine(string.Format("{0} - {1} {2}s", text, finishText, timer.ElapsedMilliseconds / 1000.0));
            }
        }

        public void Dispose()
        {
            timer.Stop();
            TraceTime("stop");
        }

        #endregion
    }
}
